﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using BusinessLogicLayer;

namespace logicunivesityinventorystore
{
    public partial class GenerateAdjustmentVoucher : System.Web.UI.Page
    {

        ItemFacade itemf = new ItemFacade();
        protected void Page_Load(object sender, EventArgs e)
        {

            lblDate.Text = DateTime.Now.ToString();

            if (gvNewAdjVoucher.Rows.Count == 0)
            {
                btnSubmit.Visible = false;
            }
            else btnSubmit.Visible = true;
            

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            btnSubmit.Visible = true;
            ArrayList description = (ArrayList)Session["popUp"];
            ArrayList sample = new ArrayList();
            foreach (string desc in description)
            {
                ItemCodeClass icc=new ItemCodeClass();
                icc.ItemCode = desc;
                sample.Add(icc);
            }
            gvNewAdjVoucher.DataSource = sample;
            gvNewAdjVoucher.DataBind();
           
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
           

            
        }

        AdjustmentVoucher adjVoucher = new AdjustmentVoucher();
        AdjustmentVoucherDetail adj = new AdjustmentVoucherDetail();
        AdjustmentVoucherFacade adjfacade = new AdjustmentVoucherFacade();
        StockCard stc = new StockCard();
        Item item = new Item();
        List<AdjustmentVoucherDetail> adjlist = new List<AdjustmentVoucherDetail>();
       
       
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            adjVoucher.ClerkID = User.Identity.Name;// (string)Session["UserName"];
            adjVoucher.DateIssue = DateTime.Now.Date;
            adjVoucher.Status = "Pending";
            adjVoucher.VoucherID = adjfacade.GetAdjustmentID(adjVoucher.ClerkID);
            adjVoucher.ApproveBy = " ";
            foreach (GridViewRow gvr in gvNewAdjVoucher.Rows)
            {
                adj.VoucherID = adjVoucher.VoucherID;
                adj.ItemCode = itemf.GetItemCodeByDesc((gvr.FindControl("lblItemDesc") as Label).Text);
                string supplierid=itemf.FindByItemID(adj.ItemCode).Supplier1;
                adj.Operation = (gvr.FindControl("ddlOperator") as DropDownList).SelectedItem.ToString();
                adj.AdjustedQty = Int32.Parse((gvr.FindControl("txtQty") as TextBox).Text);
                adj.Reason = (gvr.FindControl("txt_Reason") as TextBox).Text;
                adj.SuggestedPrice = (float)itemf.GetPrice(adj.ItemCode,supplierid)* adj.AdjustedQty;

                stc.Date = DateTime.Now.Date;
                stc.Description = adj.VoucherID;
                stc.ItemCode = adj.ItemCode;
                stc.Operation = adj.Operation;
                stc.Quantity = (int)adj.AdjustedQty;
                stc.StockCardID = itemf.GetStockCardID();
                stc.Balance = itemf.CalculateBalance(stc.ItemCode, stc.Quantity, stc.Operation);
                new ItemFacade().insertStockCard(stc);
                adjlist.Add(adj);
            }
            adjVoucher.TotalPrice=adjfacade.CalculateTotalPrice(adjlist);          
            adjfacade.SendMail(adjVoucher.ClerkID,(float)adjVoucher.TotalPrice);
            adjfacade.insertAdjustment(adjVoucher, adjlist);
            Session["popUp"] = new System.Collections.ArrayList();
            Response.Redirect("HomePage_Store.aspx");
           }
    }
}